package com.dt.common.validation.definition;

import java.math.BigDecimal;
import java.util.Map;

import com.dt.common.validation.annotation.DecimalMin;
import com.dt.common.validation.impl.DefaultValidationDefinition;
import com.dt.common.validation.itf.ConstraintValidation;
import com.dt.common.validation.itf.ValidationContext;

import lombok.Setter;

@Setter
public class DecimalMinDefinition extends DefaultValidationDefinition {
	private BigDecimal minValue;
	private boolean inclusive;

	public DecimalMinDefinition() {
		this.name = DecimalMin.class.getSimpleName();
	}

	@Override
	protected void fillMsgInterpolatingParams(Map<String, String> params) {
		params.put("value", minValue.toString());
	}

	@Override
	public ConstraintValidation buildValidation() {

		return new ConstraintValidation() {

			@Override
			public boolean isValid(ValidationContext context, Object objectValue, Object propertyValue) {
				if (propertyValue == null) {
					return true;
				}

				try {
					BigDecimal checkedValue = DecimalMaxDefinition.getCheckedValue(propertyValue);
					if (checkedValue != null) {
						if (inclusive) {
							return checkedValue.compareTo(minValue) >= 0;
						} else {
							return checkedValue.compareTo(minValue) > 0;
						}
					}else{
						return false;
					}
				} catch (NumberFormatException e) {
					return false;
				}
			}
		};

	}

}
